package com.ruoyi.project.module.cjl;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.*;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.project.domain.ISysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;
import static com.ruoyi.common.security.utils.SecurityUtils.getUserId;

/**
 * 成果信息Controller
 * 
 * @author ruoyi
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/cjl/achievement")
public class CjlCxcyAchievementController extends BaseController
{
    @Autowired
    private ICxcyAchievementService cxcyAchievementService;
    @Autowired
    private ICxcyTransformationService cxcyTransformationService;
    @Autowired
    private ISysRoleService sysRoleService;
    @Autowired
    private ICxcyBaseinfoService cxcyBaseinfoService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ICxcyContractService cxcyContractService;
    @Autowired
    private ICxcyExpenseService cxcyExpenseService;
    /**
     * 查询成果信息列表
     */
    @RequiresPermissions("project:cjl:achievement:list")
    @GetMapping("/list")
    public TableDataInfo list(CxcyAchievement cxcyAchievement)
    {
        startPage();
        Long userId = getUserId();
        SysUserRole sysRole = sysRoleService.selectRoleByUser(userId);
        Long roleId = sysRole.getRoleId();
        if(roleId.equals(13L)||roleId.equals(14L)){
            startPage();
            cxcyAchievement.setAuditState("2");
            List<CxcyAchievement> admitlist = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
            return getDataTable(admitlist);
        }
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        return getDataTable(list);
    }
    /**
     * 查询成果信息列表
     */
    @GetMapping("/Chalist")
    public TableDataInfo Chalist(CxcyAchievement cxcyAchievement) {
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);

        // 使用Stream API对list进行处理，根据userId进行分组，并取每组的第一个元素
        List<CxcyAchievement> filteredList = list.stream()
                .collect(Collectors.toMap(CxcyAchievement::getFirstNumber, Function.identity(), (existing, replacement) -> existing))
                .values()
                .stream()
                .collect(Collectors.toList());

        return getDataTable(filteredList);
    }

    /**
     * 查询成果信息列表
     */
    @GetMapping("/Yearlist")
    public AjaxResult Yearlist(CxcyAchievement cxcyAchievement) {
        String[] yearArray = {"2020", "2021", "2022", "2023", "2024", "2025", "2026", "2027"};

        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);

        ArrayList result = new ArrayList<>();

        boolean add2021 = false; // 是否已添加2021
        boolean add2022 = false; // 是否已添加2022
        boolean add2023 = false; // 是否已添加2023

        boolean add2024 = false; // 是否已添加2024
        boolean add2025 = false; // 是否已添加2025
        boolean add2026 = false; // 是否已添加2026

        boolean add2027 = false; // 是否已添加2027

        for (CxcyAchievement achievement : list) {
            if (achievement.getResultYear().equals("1") && !add2021) {
                result.add("2021");
                add2021 = true; // 标记已添加2021
            }
            if (achievement.getResultYear().equals("2") && !add2022) {
                result.add("2022");
                add2022 = true; // 标记已添加2022
            }
            if (achievement.getResultYear().equals("3") && !add2023) {
                result.add("2023");
                add2023 = true; // 标记已添加2023
            }
            if (achievement.getResultYear().equals("4") && !add2024) {
                result.add("2024");
                add2023 = true; // 标记已添加2023
            }
            if (achievement.getResultYear().equals("5") && !add2025) {
                result.add("2025");
                add2023 = true; // 标记已添加2023
            }
            if (achievement.getResultYear().equals("6") && !add2026) {
                result.add("2026");
                add2023 = true; // 标记已添加2023
            }
            if (achievement.getResultYear().equals("7") && !add2027) {
                result.add("2027");
                add2023 = true; // 标记已添加2023
            }



        }

    return AjaxResult.success(result);

//        return getDataTable(filteredList);
    }

    /**
     * 查询成果信息列表
     */
    @GetMapping("/Collegelist")
    public TableDataInfo Collegelist(CxcyAchievement cxcyAchievement) {
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);

        // 使用Stream API对list进行处理，根据userId进行分组，并取每组的第一个元素
        List<CxcyAchievement> filteredList = list.stream()
                .collect(Collectors.toMap(CxcyAchievement::getFirstCollege, Function.identity(), (existing, replacement) -> existing))
                .values()
                .stream()
                .collect(Collectors.toList());

        return getDataTable(filteredList);
    }



    @GetMapping("/list2/{userId}")
    public String[][] list2(@PathVariable("userId") Long userId)
    {
        /*System.out.println(userId+"woshi");*/
        CxcyAchievement cxcyAchievement = new CxcyAchievement();
        cxcyAchievement.setUserId(userId);
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        cxcyAchievement.setFirstNumber(list.get(0).getFirstNumber());
        List<CxcyAchievement> list1 = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        HashMap<String, String> typeMap = new HashMap<>();
        typeMap.put("0", "创新训练项目");
        typeMap.put("1", "创业训练项目");
        typeMap.put("2", "创业实践项目");

        HashMap<String, String> typeMap1 = new HashMap<>();
        typeMap1.put("0", "发明专利");
        typeMap1.put("1", "实用新型（设计）专利");
        typeMap1.put("2", "软件著作权");
        typeMap1.put("3", "技术");
        typeMap1.put("4", "其他");

        HashMap<String, String> typeMap2 = new HashMap<>();
        typeMap2.put("0", "国家级");
        typeMap2.put("1", "省级");
        typeMap2.put("2", "校级");
        typeMap2.put("3", "院级");

        HashMap<String, String> typeMap3 = new HashMap<>();
        typeMap3.put("0", "创新创业项目资助");
        typeMap3.put("1", "横向资助（企业/社会团体/基金等）");

        HashMap<String, String> typeMap4 = new HashMap<>();
        typeMap4.put("0", "项目支撑");
        typeMap4.put("1", "自由探索");

        HashMap<String, String> typeMap5 = new HashMap<>();
        typeMap5.put("0", "新能源");
        typeMap5.put("1", "新能源");
        typeMap5.put("2", "节能环保");
        typeMap5.put("3", "高端装备制造业");
        typeMap5.put("4", "新材料");
        typeMap5.put("5", "生物产业");
        typeMap5.put("6", "新医药");
        typeMap5.put("7", "新化工");
        typeMap5.put("8", "其他");

        String[][] data = new String[0][];
        for(int i=0;i<list1.size();i++){

            String applyTime = dateFormat.format(list1.get(i).getApplyTime());
            String[] rowData = {

                    String.valueOf(list1.get(i).getResultName()),
                    String.valueOf(list1.get(i).getCompletionUnit()),
                    typeMap.get(list1.get(i).getResultType()),
                    typeMap1.get(list1.get(i).getResultCategory()),
                    typeMap2.get(list1.get(i).getResultGrade()),
                    typeMap3.get(list1.get(i).getResultSource()),
                    typeMap4.get(list1.get(i).getResultBackground()),
                    typeMap5.get(list1.get(i).getTechnology()),
                    applyTime


            };
            // 扩展数组大小并将字符串值赋给新数组
            data = Arrays.copyOf(data, data.length + 1);
            data[data.length - 1] = rowData;
        }
        return data;

    }
    /**
     * 查询成果审核通过信息列表
     */
    @GetMapping("/admitlist")
    public TableDataInfo admitlist(CxcyAchievement cxcyAchievement)
    {
        startPage();
        cxcyAchievement.setAuditState("2");
        List<CxcyAchievement> list = cxcyAchievementService.selectStudentAchievementList(cxcyAchievement);
        return getDataTable(list);
    }

    /**
     * 导出成果信息列表
     */
    @RequiresPermissions("project:cjl:achievement:export")
    @Log(title = "成果信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CxcyAchievement cxcyAchievement)
    {
        List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement);
        ExcelUtil<CxcyAchievement> util = new ExcelUtil<CxcyAchievement>(CxcyAchievement.class);
        util.exportExcel(response, list, "成果信息数据");
    }

    /**
     * 获取成果信息详细信息
     */
    @RequiresPermissions("project:cjl:achievement:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cxcyAchievementService.selectCxcyAchievementById(id));
    }

    /**
     * 新增成果信息
     */
    @RequiresPermissions("project:cjl:achievement:add")
    @Log(title = "成果信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyAchievement cxcyAchievement)
    {
        if(StringUtils.isBlank(cxcyAchievement.getResultName())){
            throw new ServiceException("成果名称不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstName())){
            throw new ServiceException("负责人不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstPhone())){
            throw new ServiceException("负责人联系电话不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstGrade())){
            throw new ServiceException("负责人年级不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstMajor())){
            throw new ServiceException("负责人专业不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstNumber())){
            throw new ServiceException("负责人学号不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstCollege())){
            throw new ServiceException("负责人学院不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getFirstIdnumber())){
            throw new ServiceException("负责人身份证号不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultType())){
            throw new ServiceException("成果类型不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultCategory())){
            throw new ServiceException("成果类别不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getTechnology())){
            throw new ServiceException("技术领域不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultGrade())){
            throw new ServiceException("成果等级不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getIsShow())){
            throw new ServiceException("是否大创不能为空！");
        }
        if(StringUtils.isNull(cxcyAchievement.getProjectTime())){
            throw new ServiceException("立项时间不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getCooperativeObject())){
            throw new ServiceException("合作对象不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getCompletionUnit())){
            throw new ServiceException("完成单位不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultYear())){
            throw new ServiceException("年份不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getPicturePath1())){
            throw new ServiceException("成果图片不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getPicturePath2())){
            throw new ServiceException("授权证明图不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultIntroduction())){
            throw new ServiceException("成果情况不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultBackground())){
            throw new ServiceException("成果背景不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getResultSource())){
            throw new ServiceException("成果来源不能为空！");
        }

        if(StringUtils.isBlank(cxcyAchievement.getSecondName())){
            throw new ServiceException("成员一姓名不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getSecondCollege())){
            throw new ServiceException("成员一学院不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getSecondIdentity())){
            throw new ServiceException("成员一身份不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getSecondNumber())){
            throw new ServiceException("成员一学号/工号不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getSecondPhone())){
            throw new ServiceException("成员一联系电话不能为空！");
        }
        if(StringUtils.isBlank(cxcyAchievement.getSecondIdnumber())){
            throw new ServiceException("成员一身份证号不能为空！");
        }

        CxcyAchievement cxcyAchievement2 = new CxcyAchievement();
        cxcyAchievement2.setResultName(cxcyAchievement.getResultName());
        List<CxcyAchievement> list1 = cxcyAchievementService.selectCxcyAchievementNameList(cxcyAchievement2);
        if(list1 != null && !list1.isEmpty()) {
            if (list1.get(0).getResultName().equals(cxcyAchievement.getResultName())) {
                throw new ServiceException("成果名称重复，请重新输入！");
            }
        }else {
            // 获取当前登录用户
            LoginUser loginUser = getLoginUser();
            SysUser sysUser = loginUser.getSysUser();
            cxcyAchievement.setUserId(sysUser.getUserId());
            cxcyAchievement.setApplyName(sysUser.getNickName());
            cxcyAchievement.setApplyNumber(sysUser.getUserName());
            cxcyAchievement.setAuditState("1");
            cxcyAchievement.setSigningStatus("0");
            cxcyAchievement.setApplyTime(new Date());
            cxcyAchievementService.insertCxcyAchievement(cxcyAchievement);
            CxcyAchievement cxcyAchievement1 = new CxcyAchievement();
            cxcyAchievement1.setResultName(cxcyAchievement.getResultName());
            List<CxcyAchievement> list = cxcyAchievementService.selectCxcyAchievementList(cxcyAchievement1);
            Long iid = list.get(0).getId();
            CxcyTransformation cxcyTransformation = new CxcyTransformation();
            cxcyTransformation.setResultId(iid);
            cxcyTransformation.setUserId(sysUser.getUserId());
            cxcyTransformation.setResultName(cxcyAchievement.getResultName());
            cxcyTransformation.setResultCategory(cxcyAchievement.getResultCategory());
            cxcyTransformation.setResultIntroduction(cxcyAchievement.getResultIntroduction());
            cxcyTransformation.setCompletionUnit(cxcyAchievement.getCompletionUnit());
            cxcyTransformation.setResultSource(cxcyAchievement.getResultSource());
            cxcyTransformation.setResultBackground(cxcyAchievement.getResultBackground());
            cxcyTransformation.setFirstName(cxcyAchievement.getFirstName());
            cxcyTransformation.setFirstCollege(cxcyAchievement.getFirstCollege());
            cxcyTransformation.setFirstMajor(cxcyAchievement.getFirstMajor());
            cxcyTransformation.setFirstNumber(cxcyAchievement.getFirstNumber());
            cxcyTransformation.setFirstPhone(cxcyAchievement.getFirstPhone());
            cxcyTransformation.setFirstIdnumber(cxcyAchievement.getFirstIdnumber());
            cxcyTransformation.setSecondName(cxcyAchievement.getSecondName());
            cxcyTransformation.setSecondCollege(cxcyAchievement.getSecondCollege());
            cxcyTransformation.setSecondIdentity(cxcyAchievement.getSecondIdentity());
            cxcyTransformation.setSecondMajor(cxcyAchievement.getSecondMajor());
            cxcyTransformation.setSecondNumber(cxcyAchievement.getSecondNumber());
            cxcyTransformation.setSecondPhone(cxcyAchievement.getSecondPhone());
            cxcyTransformation.setSecondIdnumber(cxcyAchievement.getSecondIdnumber());
            cxcyTransformation.setThirdName(cxcyAchievement.getThirdName());
            cxcyTransformation.setThirdCollege(cxcyAchievement.getThirdCollege());
            cxcyTransformation.setThirdIdentity(cxcyAchievement.getThirdIdentity());
            cxcyTransformation.setThirdMajor(cxcyAchievement.getThirdMajor());
            cxcyTransformation.setThirdNumber(cxcyAchievement.getThirdNumber());
            cxcyTransformation.setThirdPhone(cxcyAchievement.getThirdPhone());
            cxcyTransformation.setThirdIdnumber(cxcyAchievement.getThirdIdnumber());
            cxcyTransformation.setFourthName(cxcyAchievement.getFourthName());
            cxcyTransformation.setFourthCollege(cxcyAchievement.getFourthCollege());
            cxcyTransformation.setFourthIdentity(cxcyAchievement.getFourthIdentity());
            cxcyTransformation.setFourthMajor(cxcyAchievement.getFourthMajor());
            cxcyTransformation.setFourthNumber(cxcyAchievement.getFourthNumber());
            cxcyTransformation.setFourthPhone(cxcyAchievement.getFourthPhone());
            cxcyTransformation.setFourthIdnumber(cxcyAchievement.getFourthIdnumber());
            cxcyTransformation.setFifthName(cxcyAchievement.getFifthName());
            cxcyTransformation.setFifthCollege(cxcyAchievement.getFifthCollege());
            cxcyTransformation.setFifthIdentity(cxcyAchievement.getFifthIdentity());
            cxcyTransformation.setFifthMajor(cxcyAchievement.getFifthMajor());
            cxcyTransformation.setFifthNumber(cxcyAchievement.getFifthNumber());
            cxcyTransformation.setFifthPhone(cxcyAchievement.getFifthPhone());
            cxcyTransformation.setFifthIdnumber(cxcyAchievement.getFifthIdnumber());
            cxcyTransformation.setSixthName(cxcyAchievement.getSixthName());
            cxcyTransformation.setSixthCollege(cxcyAchievement.getSixthCollege());
            cxcyTransformation.setSixthIdentity(cxcyAchievement.getSixthIdentity());
            cxcyTransformation.setSixthMajor(cxcyAchievement.getSixthMajor());
            cxcyTransformation.setSixthNumber(cxcyAchievement.getSixthNumber());
            cxcyTransformation.setSixthPhone(cxcyAchievement.getSixthPhone());
            cxcyTransformation.setSixthIdnumber(cxcyAchievement.getSixthIdnumber());
            cxcyTransformation.setNote2(cxcyAchievement.getNote1());
            cxcyTransformation.setSigningStatus("0");
            cxcyTransformation.setAuditState("0");
            cxcyTransformationService.insertCxcyTransformation(cxcyTransformation);
        }
        ISysUser User = new ISysUser();
        User.setRemark("teacher");
        List<ISysUser> list2 = userService.selectMsgUserList(User);
        for (ISysUser user:list2) {
            CjlWeChatMassage.sendWeChatMassage(cxcyAchievement, user);
        }
        return success();

    }

    /**
     * 获取用户权限信息
     */
    @GetMapping("/roleId")
    public Long roleId() {
        // 获取当前用户信息
        Long userId = getUserId();

        SysUserRole sysRole = sysRoleService.selectRoleByUser(userId);
        Long roleId = sysRole.getRoleId();
        //System.out.println(roleId);

        return roleId;
    }

    /**
     * 修改成果信息
     */
    @RequiresPermissions("project:cjl:achievement:edit")
    @Log(title = "成果信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyAchievement cxcyAchievement)
    {
        CxcyTransformation cxcyTransformation = new CxcyTransformation();
        cxcyTransformation.setResultId(cxcyAchievement.getId());
        List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);
        for(CxcyTransformation tran: list){
            tran.setResultName(cxcyAchievement.getResultName());
            tran.setResultCategory(cxcyAchievement.getResultCategory());
            tran.setResultIntroduction(cxcyAchievement.getResultIntroduction());
            tran.setCompletionUnit(cxcyAchievement.getCompletionUnit());
            tran.setResultSource(cxcyAchievement.getResultSource());
            tran.setResultBackground(cxcyAchievement.getResultBackground());
            tran.setFirstName(cxcyAchievement.getFirstName());
            tran.setFirstCollege(cxcyAchievement.getFirstCollege());
            tran.setFirstMajor(cxcyAchievement.getFirstMajor());
            tran.setFirstNumber(cxcyAchievement.getFirstNumber());
            tran.setFirstPhone(cxcyAchievement.getFirstPhone());
            tran.setFirstIdnumber(cxcyAchievement.getFirstIdnumber());
            tran.setSecondName(cxcyAchievement.getSecondName());
            tran.setSecondCollege(cxcyAchievement.getSecondCollege());
            tran.setSecondIdentity(cxcyAchievement.getSecondIdentity());
            tran.setSecondMajor(cxcyAchievement.getSecondMajor());
            tran.setSecondNumber(cxcyAchievement.getSecondNumber());
            tran.setSecondPhone(cxcyAchievement.getSecondPhone());
            tran.setSecondIdnumber(cxcyAchievement.getSecondIdnumber());
            tran.setThirdName(cxcyAchievement.getThirdName());
            tran.setThirdCollege(cxcyAchievement.getThirdCollege());
            tran.setThirdIdentity(cxcyAchievement.getThirdIdentity());
            tran.setThirdMajor(cxcyAchievement.getThirdMajor());
            tran.setThirdNumber(cxcyAchievement.getThirdNumber());
            tran.setThirdPhone(cxcyAchievement.getThirdPhone());
            tran.setThirdIdnumber(cxcyAchievement.getThirdIdnumber());
            tran.setFourthName(cxcyAchievement.getFourthName());
            tran.setFourthCollege(cxcyAchievement.getFourthCollege());
            tran.setFourthIdentity(cxcyAchievement.getFourthIdentity());
            tran.setFourthMajor(cxcyAchievement.getFourthMajor());
            tran.setFourthNumber(cxcyAchievement.getFourthNumber());
            tran.setFourthPhone(cxcyAchievement.getFourthPhone());
            tran.setFourthIdnumber(cxcyAchievement.getFourthIdnumber());
            tran.setFifthName(cxcyAchievement.getFifthName());
            tran.setFifthCollege(cxcyAchievement.getFifthCollege());
            tran.setFifthIdentity(cxcyAchievement.getFifthIdentity());
            tran.setFifthMajor(cxcyAchievement.getFifthMajor());
            tran.setFifthNumber(cxcyAchievement.getFifthNumber());
            tran.setFifthPhone(cxcyAchievement.getFifthPhone());
            tran.setFifthIdnumber(cxcyAchievement.getFifthIdnumber());
            tran.setSixthName(cxcyAchievement.getSixthName());
            tran.setSixthCollege(cxcyAchievement.getSixthCollege());
            tran.setSixthIdentity(cxcyAchievement.getSixthIdentity());
            tran.setSixthMajor(cxcyAchievement.getSixthMajor());
            tran.setSixthNumber(cxcyAchievement.getSixthNumber());
            tran.setSixthPhone(cxcyAchievement.getSixthPhone());
            tran.setSixthIdnumber(cxcyAchievement.getSixthIdnumber());
            cxcyTransformationService.updateCxcyTransformation(tran);
        }
        return toAjax(cxcyAchievementService.updateCxcyAchievement(cxcyAchievement));
    }

    /**
     * 删除成果信息
     */
    @RequiresPermissions("project:cjl:achievement:remove")
    @Log(title = "成果信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cxcyAchievementService.deleteCxcyAchievementByIds(ids));
    }

    /**
     * 审核成果
     */
    @RequiresPermissions("project:cjl:achievement:aduit")
    @Log(title = "审核信息", businessType = BusinessType.UPDATE)
    @PutMapping("/aduit")
    public AjaxResult aduit(@RequestBody CxcyAchievement cxcyAchievement) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyAchievement.setAuditName(sysUser.getNickName());
        cxcyAchievement.setAuditNumber(sysUser.getUserName());
        cxcyAchievement.setAuditTime(new Date());
        CjlWeChatAuditMassage.sendWeChatAuditMassage(cxcyAchievement);
        return toAjax(cxcyAchievementService.updateCxcyAchievement(cxcyAchievement));
    }

    /**
     * 查询认证
     */
    @Log(title = "认证信息", businessType = BusinessType.UPDATE)
    @GetMapping("/cerification")
    public int cerification() {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        CxcyBaseinfo cxcyBaseinfo=new CxcyBaseinfo();
        cxcyBaseinfo.setUserId(sysUser.getUserId());
        int p=0;
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        for(CxcyBaseinfo a:list) {
            if(a.getAuditState().equals("1")||a.getAuditState().equals("3")){
                p=1;
                return p;
            }
        }
        return p;
    }

    /**
     * 查询企业是否完善信息
     */
    @Log(title = "企业是否完善信息", businessType = BusinessType.UPDATE)
    @GetMapping("/checksign")
    public int sign1() {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        CxcyBaseinfo cxcyBaseinfo=new CxcyBaseinfo();
        cxcyBaseinfo.setUserId(sysUser.getUserId());
        int q=0;
        List<CxcyBaseinfo> list1 = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        for(CxcyBaseinfo b:list1) {
            if(b.getBusinessLicense()==null||b.getBusinessLicense().equals(" ")){
                q=1;
                return q;
            }
        }
       return q;
    }

    /**
     * 查询企业或个人
     */
    @Log(title = "企业或个人信息", businessType = BusinessType.UPDATE)
    @GetMapping("/allperson")
    public int allperson() {
        // 获取当前登录用户
        //UsetType： 1是企业，2是用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        CxcyBaseinfo cxcyBaseinfo=new CxcyBaseinfo();
        cxcyBaseinfo.setUserId(sysUser.getUserId());
        int r=0;
        List<CxcyBaseinfo> list = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        for(CxcyBaseinfo a:list) {
            if(a.getUserType().equals("2")){
                r=1;
                return r;
            }
        }
        return r;
    }

    /**
     * 签约
     */
    @RequiresPermissions("project:cjl:achievement:sign")
    @Log(title = "签约信息", businessType = BusinessType.UPDATE)
    @PutMapping("/sign")
    public AjaxResult sign(@RequestBody CxcyBaseinfo cxcyBaseinfo) {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        CxcyBaseinfo cxcyBaseinfo1=new CxcyBaseinfo();
        cxcyBaseinfo1.setUserId(sysUser.getUserId());
        List<CxcyBaseinfo> list1 = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo1);
        for(CxcyBaseinfo b:list1){
            b.setCorporateName(cxcyBaseinfo.getCorporateName());
            b.setCorporateEmail(cxcyBaseinfo.getCorporateEmail());
            b.setCorporatePhone(cxcyBaseinfo.getCorporatePhone());
            b.setCorporateNumber(cxcyBaseinfo.getCorporateNumber());
            b.setBusinessLicense(cxcyBaseinfo.getBusinessLicense());
            b.setLicenseAddress(cxcyBaseinfo.getLicenseAddress());
            b.setEnterpriseAddress(cxcyBaseinfo.getEnterpriseAddress());
            b.setNote3(cxcyBaseinfo.getNote3());
            cxcyBaseinfoService.updateCxcyBaseinfo(b);
            CxcyTransformation cxcyTransformation = new CxcyTransformation();
            cxcyTransformation.setResultId(cxcyBaseinfo.getId());
            List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);
            for(CxcyTransformation a:list){
                a.setEnterpriseNumber(b.getEnterpriseNumber());
                a.setEnterpriseName(b.getEnterpriseName());
                a.setCorporateName(b.getCorporateName());
                a.setCorporatePhone(b.getCorporatePhone());
                a.setCorporateNumber(b.getCorporateNumber());
                a.setSigningStatus("1");
                a.setNote1(b.getUserType());
                cxcyTransformationService.updateCxcyTransformation(a);
            }
            CxcyAchievement cxcyAchievement=new CxcyAchievement();
            cxcyAchievement.setId(cxcyBaseinfo.getId());
            cxcyAchievement.setSigningStatus("1");
            cxcyAchievementService.updateCxcyAchievement(cxcyAchievement);
        }
        return success();
    }

    /**
     * 完善信息后的签约
     */
    @Log(title = "成果信息", businessType = BusinessType.UPDATE)
    @GetMapping("/outsign/{id}")
    public AjaxResult sign2(@PathVariable("id") Long id)
    {
        CxcyAchievement cxcyAchievement=new CxcyAchievement();
        cxcyAchievement.setId(id);
        cxcyAchievement.setSigningStatus("1");
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        CxcyBaseinfo cxcyBaseinfo=new CxcyBaseinfo();
        cxcyBaseinfo.setUserId(sysUser.getUserId());
        List<CxcyBaseinfo> list1 = cxcyBaseinfoService.selectCxcyBaseinfoList(cxcyBaseinfo);
        for(CxcyBaseinfo b:list1) {
            CxcyTransformation cxcyTransformation = new CxcyTransformation();
            cxcyTransformation.setResultId(id);
            List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);
            for (CxcyTransformation a : list) {
                a.setEnterpriseNumber(b.getEnterpriseNumber());
                a.setEnterpriseName(b.getEnterpriseName());
                a.setCorporateName(b.getCorporateName());
                a.setCorporatePhone(b.getCorporatePhone());
                a.setCorporateNumber(b.getCorporateNumber());
                a.setNote1(b.getUserType());
                a.setSigningStatus("1");
                cxcyTransformationService.updateCxcyTransformation(a);
            }
        }
        return toAjax(cxcyAchievementService.updateCxcyAchievement(cxcyAchievement));
    }
    /**
     *取消签约
     */
    @Log(title = "成果信息", businessType = BusinessType.UPDATE)
    @GetMapping("/Cancellation/{id}")
    public AjaxResult Cancellation(@PathVariable("id") Long id){
        CxcyAchievement cxcyAchievement=new CxcyAchievement();
        cxcyAchievement.setId(id);
        cxcyAchievement.setSigningStatus("0");

        CxcyTransformation cxcyTransformation = new CxcyTransformation();
        cxcyTransformation.setResultId(id);
        List<CxcyTransformation> Translist = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);
        if(Translist.isEmpty()){}
        else {
            Long Transid = Translist.get(0).getId();
            cxcyTransformation.setId(Transid);
            cxcyTransformation.setFileId("");
            cxcyTransformation.setNumberYears("");
            cxcyTransformation.setTransformationMode("");
            cxcyTransformation.setPatentSoftware("");
            cxcyTransformation.setObligee("");
            cxcyTransformation.setFundingStatement("");
            cxcyTransformation.setTenureConfidentiality("");
            cxcyTransformation.setHistoricalTransaction("");
            cxcyTransformation.setCorrelationCondition("");
            cxcyTransformation.setLicenseMode("");
            cxcyTransformation.setModeTransfer("");
            cxcyTransformation.setPaymentAmount1("");//第一次（万元）方式不能为空！
            /*cxcyTransformation.setAcceptTime(null);//受理时间不能为空*/
           /* cxcyTransformation.setPaymentTime1(null);*/
            cxcyTransformation.setPaymentAmount2("");//第一次（万元）方式不能为空！
           /* cxcyTransformation.setPaymentTime2(null);*/
            cxcyTransformation.setPaymentAmount3("");//第一次（万元）方式不能为空！
            /*cxcyTransformation.setPaymentTime3(null);*/
            cxcyTransformation.setPaymentAmount4("");//第一次（万元）方式不能为空！
           /* cxcyTransformation.setPaymentTime4(null);*/
            cxcyTransformation.setPayArrangement("");
            cxcyTransformation.setEnterpriseNumber("");
            cxcyTransformation.setEnterpriseName("");
            cxcyTransformation.setCorporateName("");
            cxcyTransformation.setCorporatePhone("");
            cxcyTransformation.setCorporateNumber("");
            cxcyTransformation.setAuditState("0");
            cxcyTransformation.setSigningStatus("0");
            cxcyTransformation.setTemplatePath("");
            cxcyTransformation.setPacketPath("");
            cxcyTransformation.setAuditName("");
            cxcyTransformation.setAuditNumber("");
            cxcyTransformation.setApplyName("");
            cxcyTransformation.setApplyNumber("");
            /*cxcyTransformation.setApplyTime(null);*/
            cxcyTransformation.setNote1("");
            cxcyTransformationService.updateCxcyTransformation(cxcyTransformation);//删除转化表
        }
        CxcyContract cxcyContract = new CxcyContract();
        cxcyContract.setResultId(id);
        List<CxcyContract> Contractlist =cxcyContractService.selectCxcyContractList(cxcyContract);
        if(Contractlist.isEmpty()){
        }else {
            Long Contractid = Contractlist.get(0).getId();
            cxcyContractService.deleteCxcyContractById(Contractid);//删除合同表
        }
        CxcyExpense cxcyExpense = new CxcyExpense();
        cxcyExpense.setResultId(id);
        List<CxcyExpense> Expenselist = cxcyExpenseService.selectCxcyExpenseList(cxcyExpense);
        if(Expenselist.isEmpty()) {}
        else{
            Long Expenseid = Expenselist.get(0).getId();
            cxcyExpenseService.deleteCxcyExpenseById(Expenseid);//删除费用表
        }
        return toAjax(cxcyAchievementService.updateCxcyAchievement(cxcyAchievement));
    }


    /**
     * 查看
     */
    @GetMapping(value = "/view/{userId}")
    public AjaxResult view(@PathVariable("userId") Long userId)
    {
        /*System.out.println(userId+"222");*/
        List<CxcyAchievement> list1 = cxcyAchievementService.selectCxcyAchievementByUserId(userId);
        /*System.out.println(list1.get(0)+"222");*/
        return AjaxResult.success(list1.get(0));

    }

    /**
     * 关闭
     */
    @GetMapping(value = "/close")
    public AjaxResult closen()
    {
        return success("0");

    }
}

